package com.hzdl.log;


import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;


@Component
@Aspect
public class TransactionManager {

    @Pointcut(value = "execution(* com.hzdl.service.impl.AccountServiceImpl.*(..))")
    public void pt(){};

    @Before("pt()")
    public void startTransaction() {
        System.out.println("开启事物");
    }


    public void commitTransaction() {
        System.out.println("提交事物");
    }

    public void rollbackTransaction() {
        System.out.println("回滚事物");
    }


    public void finalTransaction() {
        System.out.println("关闭事物");
    }

    /*
     * ProceedingJoinPoint 对象来帮我们执行method.invoke方法，返回值就理应返回出去
     * */
    public Object aroundTransaction(ProceedingJoinPoint joinPoint) {

        Object[] args = joinPoint.getArgs();
        Object proceed = null;
        try {
            System.out.println("开启事物");
            proceed = joinPoint.proceed(args);
            System.out.println("提交事物");
        } catch (Throwable throwable) {
            System.out.println("回滚事物");
            throwable.printStackTrace();
        } finally {
            System.out.println("关闭事物");

        }
        return proceed;


    }

}
